package cn.mall.order.web;

import cn.mall.cart.to.UserInfoTo;
import cn.mall.order.interceptor.LoginUserInterceptor;
import cn.mall.order.service.OrderService;
import cn.mall.order.vo.OrderConfirmVo;
import cn.mall.order.vo.OrderSubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;

import static cn.mall.common.constant.CartConstant.CART_PREFIX;

@Controller
public class OrderWebController {
//    在购物车中点击结算跳转到confirm结算页面的请求
    @Autowired
    OrderService orderService;//能返回订单确认页所需要的数据
    @GetMapping("/toTrade")
    public  String toTrade(Model model, HttpServletRequest request){
        UserInfoTo userInfoTo = LoginUserInterceptor.userKey.get();


        String cartKey =CART_PREFIX+userInfoTo.getUserId();
        System.out.println("Order远程调用得到userKey = " + cartKey);
        //希望orderService能够返回一个方法给订单确认页
        //confirmOrder()方法最终返回到orderConfirmVo
        OrderConfirmVo confirmVo=orderService.confirmOrder(cartKey);
        System.out.println("confirmVo = " + confirmVo);
        model.addAttribute("orderConfirmData",confirmVo);
        //展示订单确认的数据
        return "confirm";
    }
    //提交订单里的数据，下单功能
    @PostMapping("/submitOrder")
    public String submitOrder(Model model){
        //下单，去创建订单，验令牌，验价格，锁库存...
        //下单成功来到支付选择页
        UserInfoTo userInfoTo = LoginUserInterceptor.userKey.get();

        String cartKey =CART_PREFIX+userInfoTo.getUserId();
        OrderConfirmVo confirmVo=orderService.confirmOrder(cartKey);
        System.out.println("confirmVo = " + confirmVo);
        model.addAttribute("orderConfirmData",confirmVo);
        return "pay";

    }
    @GetMapping("/listOrder")
    public  String listOrder(Model model){
        UserInfoTo userInfoTo = LoginUserInterceptor.userKey.get();

        String cartKey =CART_PREFIX+userInfoTo.getUserId();
        //希望orderService能够返回一个方法给订单确认页
        //confirmOrder()方法最终返回到orderConfirmVo
        OrderConfirmVo confirmVo=orderService.confirmOrder(cartKey);
        System.out.println("confirmVo = " + confirmVo);
        model.addAttribute("orderConfirmData",confirmVo);
        return "list";

    }
}


